package com.trust.pams.consumer.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.chasonx.common.interceptor.ParamData;
import com.chasonx.common.interceptor.RequiredParam;
import com.chasonx.common.interceptor.RouterSafety;
import com.chasonx.common.util.DurationCode;
import com.chasonx.common.util.Ret;
import com.trust.smart.uuc.model.ao.LoginModel;
import com.trust.pams.util.CacheUtil;
import com.trust.smart.uuc.model.vo.LoginRet;
import com.trust.smart.uuc.model.vo.LoginUserInfo;
import com.trust.smart.uuc.service.provider.ILoginProvider;
import com.trust.smart.uuc.util.LoginStatus;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/pams/loginRelated")
@Api(value = "loginRelated")
public class LoginConsumerController {

	 private static final Logger log = LoggerFactory.getLogger(LoginConsumerController.class);

	 @Reference(timeout = 60000 )
	 private ILoginProvider loginProvider;

	 @RouterSafety(
	            requestTime = 50,
	            maxRequestTime = 70,
	            joinBlacklist = true,
	            blacklistDuration = DurationCode.ONE_MINUTE
	    )
	 
	 @ApiOperation(value = "登录", notes = "1")
	 @RequestMapping(value="/login", method = RequestMethod.POST)
	 public Ret login(ParamData<LoginModel> param) {
	     Ret ret = Ret.get();
	     LoginModel paramObj = param.getData();
	     try{
	            LoginRet loginret = loginProvider.login(paramObj);
	            CacheUtil.removeCaptchaCode(paramObj.getCaptchaToken());
	            if(LoginStatus.CaptchaExpire == loginret.getStatus()){
	                ret.faild("验证码失效，请重新输入");
	                return ret;
	            }
	            if(LoginStatus.CheckCaptchaFaild== loginret.getStatus()){
	                ret.faild("验证码输入有误，请重新输入");
	                return ret;
	            }
	            if(LoginStatus.CheckUserFaild== loginret.getStatus() ){
	                ret.faild("用户名或密码错误");
	                return ret;
	            }

	            if(LoginStatus.InvalidUser== loginret.getStatus()){
	                return ret.faild("该账号已被冻结");
	            }

	            if(LoginStatus.InvalidRole== loginret.getStatus()){
	                return ret.faild("当前角色无效，登录失败");
	            }
	            Map<String,Object> retMap = new HashMap<String,Object>();
	            if(LoginStatus.Success ==  loginret.getStatus()) {
	            	 retMap.put("info" , loginret.getUserInfo() );
	                 retMap.put("token" ,loginret.getToken());
	                 ret.setData( retMap );
	            }
	            if(LoginStatus.Exception ==  loginret.getStatus()) {
	            	  return ret.faild("登录异常，请稍后再试");
	            }
	     }catch (Exception e) {
	    	 ret.error();
	         log.error("{} login exception : ", paramObj.getLoginName() , e);
		}
		return ret;
	 }
	 
	@ApiOperation(value = "登出", notes = "2")
	@RequestMapping(value="/logout", method = RequestMethod.POST)
	public boolean logout(@RequestBody LoginUserInfo loginUserInfo) {
		boolean flag= loginProvider.logout(loginUserInfo);
		if(flag) {
			return flag;
		}else {
			return flag;
		}
	}
	 
}
